package shanghaielectric_lib.Utils.FileUtils;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import shanghaielectric_lib.Utils.DateUtil;

import static android.content.Context.MODE_APPEND;

/**
 * Created by 45000460 on 2017/12/29.
 */

public class TxtUtil {
    public static void writeLogToFile(String path,String fileName,String context){
        File file=new File(path+"/"+fileName);
        PrintWriter pfp= null;
        try {
            pfp = new PrintWriter(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (pfp != null) {
            pfp.print(DateUtil.getSystemTime_yyyy_MM_dd_HH_mm_ss()+context);
            pfp.close();
        }
    }
    public static void writeLogToFile2(String path,String fileName,String context) {
        FileWriter fileWriter = null;
        try {
            String mPath=path + "/" +fileName+getSystemTime_yyyy_MM()+".txt";
            File file=new File(mPath);
            if(!file.exists())
            {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            fileWriter = new FileWriter(file, true);
        } catch (IOException e) {
//            Log.e("she","IOException"+e.toString());
            e.printStackTrace();
        }
        try {
            if (fileWriter != null) {
                fileWriter.write(DateUtil.getSystemTime_yyyy_MM_dd_HH_mm_ss()+"  "+context+"\r\n");
                fileWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String getSystemTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日_HH时mm分ss秒");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }
    public static String getSystemTime_yyyy_MM() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
        Date curDate = new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }
}
